package com.uenpay.im.panel;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.publicnumber.PublicNumMenu;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-4-12 下午7:56:47
 * @copyRight http://www.qiyunxin.com
 * @function 面板中公众号管理
 */
public class PanelPublicNumManager {

	/**
	 * kayboadFrameLayout： 输入框及其他布局,publicNumLayout: 公众号布局
	 */
	private LinearLayout kayboadFrameLayout, publicNumLayout;
	/**
	 * 公众号和上下切换ImageView
	 */
	private ImageView publicNumSoft;
	/**
	 * 分割线
	 */
	private View public_num_view;
	/**
	 * item
	 */
	private TextView publicNumMenuTv1, publicNumMenuTv2, publicNumMenuTv3;
	private PanelManagerInterface.IPublicNumOnClick mISendPublicNumOnClick;
	private Context mContext;
	private MyPopupWindowBottom myPopupWindowBottom = null;

	/**
	 * 公众号键盘管理
	 * 
	 * @param _Context
	 * @param _kayboadFrameLayout
	 *            输入框及其他布局
	 * @param _publicNumLayout
	 *            公众号布局
	 * @param _publicNumSoft
	 *            公众号和上下切换ImageView
	 * @param _public_num_view
	 *            分割线
	 * @param _publicNumMenuTv1
	 *            item1
	 * @param _publicNumMenuTv2
	 *            item2
	 * @param _publicNumMenuTv3
	 *            item3
	 */
	public PanelPublicNumManager(Context _Context,
			LinearLayout _kayboadFrameLayout, LinearLayout _publicNumLayout,
			ImageView _publicNumSoft, View _public_num_view,
			TextView _publicNumMenuTv1, TextView _publicNumMenuTv2,
			TextView _publicNumMenuTv3) {
		kayboadFrameLayout = _kayboadFrameLayout;
		publicNumLayout = _publicNumLayout;
		publicNumSoft = _publicNumSoft;
		public_num_view = _public_num_view;
		publicNumMenuTv1 = _publicNumMenuTv1;
		publicNumMenuTv2 = _publicNumMenuTv2;
		publicNumMenuTv3 = _publicNumMenuTv3;
		mContext = _Context;
	}

	/**
	 * 设置公众号菜单
	 * 
	 * @param arrayList
	 */
	public void setPublicNumMenu(final ArrayList<PublicNumMenu> arrayList) {

		publicNumMenuTv1.setText(arrayList.get(0).text);
		if (arrayList.get(0).arrayList == null
				|| arrayList.get(0).arrayList.size() < 1) {
			publicNumMenuTv1.setBackgroundResource(R.drawable.layout_select_bg);
		} else {
			publicNumMenuTv1.setBackgroundResource(R.drawable.menu_selector);
		}

		publicNumMenuTv2.setText(arrayList.get(1).text);
		if (arrayList.get(1).arrayList == null
				|| arrayList.get(1).arrayList.size() < 1) {
			publicNumMenuTv2.setBackgroundResource(R.drawable.layout_select_bg);
		} else {
			publicNumMenuTv2.setBackgroundResource(R.drawable.menu_selector);
		}

		publicNumMenuTv3.setText(arrayList.get(2).text);
		if (arrayList.get(2).arrayList == null
				|| arrayList.get(2).arrayList.size() < 1) {
			publicNumMenuTv3.setBackgroundResource(R.drawable.layout_select_bg);
		} else {
			publicNumMenuTv3.setBackgroundResource(R.drawable.menu_selector);
		}

		publicNumMenuTv1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if ((arrayList.get(0).arrayList == null || arrayList.get(0).arrayList
						.size() < 1)
						&& !TextUtils.isEmpty(arrayList.get(0).url)) {
					if (mISendPublicNumOnClick != null) {
						mISendPublicNumOnClick.onPublicNumClick(arrayList
								.get(0).url);
					}
				} else {
					showPopu(publicNumMenuTv1, arrayList.get(0).arrayList);
				}
			}
		});
		publicNumMenuTv2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if ((arrayList.get(1).arrayList == null || arrayList.get(1).arrayList
						.size() < 1)
						&& !TextUtils.isEmpty(arrayList.get(1).url)) {
					if (mISendPublicNumOnClick != null) {
						mISendPublicNumOnClick.onPublicNumClick(arrayList
								.get(1).url);
					}
				} else {
					showPopu(publicNumMenuTv2, arrayList.get(1).arrayList);
				}
			}
		});
		publicNumMenuTv3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if ((arrayList.get(2).arrayList == null || arrayList.get(2).arrayList
						.size() < 1)
						&& !TextUtils.isEmpty(arrayList.get(2).url)) {
					if (mISendPublicNumOnClick != null) {
						mISendPublicNumOnClick.onPublicNumClick(arrayList
								.get(2).url);
					}
				} else {
					showPopu(publicNumMenuTv3, arrayList.get(2).arrayList);
				}
			}
		});
		publicNumSoft.setVisibility(View.VISIBLE);
		public_num_view.setVisibility(View.VISIBLE);
		kayboadFrameLayout.setVisibility(View.GONE);
		publicNumLayout.setVisibility(View.VISIBLE);
	}

	/**
	 * 显示底部popu
	 * 
	 * @param view
	 * @param arrayList
	 */
	private void showPopu(View view, ArrayList<PublicNumMenu> arrayList) {
		myPopupWindowBottom = new MyPopupWindowBottom(mContext, arrayList,
				new MyPopupWindowBottom.IPublicNumClickListener() {

					@Override
					public void onPublicNumClick(String url) {
						if (mISendPublicNumOnClick != null) {
							mISendPublicNumOnClick.onPublicNumClick(url);
						}
					}
				});
		myPopupWindowBottom.show(view);
	}

	/**
	 * 设置公众号点击回调
	 * 
	 * @param _ISendPublicNumOnClick
	 */
	public void setPublicOnclick(PanelManagerInterface.IPublicNumOnClick _ISendPublicNumOnClick) {
		mISendPublicNumOnClick = _ISendPublicNumOnClick;
	}

}
